package com.honglv.controller.common;

import com.honglv.context.BaseContext;
import com.honglv.exception.BaseException;
import com.honglv.pojo.dto.PageByCityDTO;
import com.honglv.pojo.dto.GetCommodityDTO;
import com.honglv.pojo.dto.PageDTO;
import com.honglv.pojo.dto.UpdateAcctractionDTO;
import com.honglv.pojo.dto.UpdateCommodityDTO;
import com.honglv.pojo.result.Result;
import com.honglv.pojo.vo.GetAllCommoditiesVO;
import com.honglv.pojo.vo.GetCommoditiesByCityVO;
import com.honglv.pojo.vo.GetCommodityVO;
import com.honglv.pojo.vo.PageVO;

import com.honglv.service.CommodityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@Api("文创相关接口")
@RequestMapping("/commodity")
public class CommodityController {
    @Autowired
    private CommodityService commodityService;

    /**
     * 根据城市分页获取文创
     * @param
     * @return
     */
    @GetMapping("/getCommoditiesByCity")
    @ApiOperation("根据城市分页获取文创")
    public Result<PageVO<GetCommoditiesByCityVO>> getCommoditiesByCity(PageByCityDTO pageByCityDTO){
        if (pageByCityDTO == null || pageByCityDTO.getPageSize() == null || pageByCityDTO.getPage() == null){
            throw new BaseException("参数为空");
        }
        PageVO<GetCommoditiesByCityVO> pageVO = commodityService.getCommoditiesByCity(pageByCityDTO);
        if (pageVO == null){
            return Result.success("暂无文创数据");
        }
        return Result.success(pageVO);
    }

    @GetMapping("/getCommodity")
    @ApiOperation("获取文创详情页")
    public Result<GetCommodityVO> getCommodity(GetCommodityDTO getCommodityDTO){
        if(getCommodityDTO == null || getCommodityDTO.getId() == null){
            throw new BaseException("参数为空");
        }
        GetCommodityVO getCommodityVO= commodityService.getCommodity(getCommodityDTO.getId());
        if (getCommodityVO == null){
            return Result.success("无该文创数据");
        }
        return Result.success(getCommodityVO);
    }

    @GetMapping("/getCommoditiesByAuthorId")
    @ApiOperation("通过用户id获取文创")
    public Result<List<GetAllCommoditiesVO>> getCommoditiesByAuthorId(){
        Long userId = BaseContext.getCurrentId();
        if (userId == null){
            throw new BaseException("用户未登录");
        }
        List<GetAllCommoditiesVO> list = commodityService.getCommoditiesByAuthorId(userId);
        if (list == null){
            return Result.success("用户未写作文创");
        }
        return Result.success(list);
    }


    @GetMapping("/deleteCommodities/{id}")
    @ApiOperation("删除文创")
    public Result deleteCommodities(@PathVariable Long id){
        return commodityService.deleteCommodity(id);
    }

    @GetMapping("/updateCommodities")
    @ApiOperation("修改文创信息")
    public Result updateCommodities(UpdateCommodityDTO updateCommodityDTO){
        return commodityService.updateCommodity(updateCommodityDTO);
    }


}
